package com.wxc.list;

/**
 * @program: LoveDataStructures1
 * @description: 线性结构抽象类
 * @author: wxc
 * @create: 2021-04-03 21:38
 **/
public abstract class AbsList<E> implements IList<E> {
    protected int size;

    protected void outOfBounds(int index){
        throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + size);
    }

    protected void rangeCheck(int index){
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index){
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean contains(E element) {
        return indexOf(element) != ELEMENT_NOT_FOUND;
    }

    public void add(E element) {
        add(size, element);
    }
}
